﻿using System.Collections;
using Lucene.Net.Documents;
using Lucene.Net.Highlight;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using System.Collections.Generic;
using Lucene.Net.Analysis;
using java.io;
using YurowDict;

namespace Util
{

    /// <summary>
    /// 全文搜索工具类
    /// </summary>
    public class SearchUtil
    {

        //添加文档
        public static void addDocument(
                                        string indexId, /* 文档索引ID */
                                        string title, /* 文档名称 */
                                        string description, /* 描述 */
                                        string content /* 文档内容 */
                                    )
        {
            //中文词典
            YurowAnalyzer.YurowAnalyzer y = new YurowAnalyzer.YurowAnalyzer(@"C:\dicts");
            //索引保存路径
            IndexWriter writer = new IndexWriter("C:\\index", y, true);

            Document document = new Document();
            document.Add(new Field("indexId", indexId, Field.Store.YES, Field.Index.TOKENIZED));
            document.Add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
            document.Add(new Field("description", description, Field.Store.YES, Field.Index.TOKENIZED));
            document.Add(new Field("content", content, Field.Store.YES, Field.Index.TOKENIZED));
            writer.AddDocument(document);
            writer.Close();
        }

        //全文检索
        public static Hashtable searchDocument(string queryWords, string preTag, string postTag, int maxWords)
        {
            //中文词典
            YurowAnalyzer.YurowAnalyzer y = new YurowAnalyzer.YurowAnalyzer(@"C:\dicts");
            Hashtable ht = new Hashtable();

            IndexSearcher searcher = new IndexSearcher("C:\\index");
            MultiFieldQueryParser parser = new MultiFieldQueryParser(new string[] { "title", "description", "content" }, y);
            Query query = parser.Parse(queryWords);
            SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter(preTag, postTag);
            Hits hits = searcher.Search(query);
            Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));
            highlighter.SetTextFragmenter(new SimpleFragmenter(maxWords));
            Document doc;
            string indexIdStr = "";
            string searchContent = "";
            int size = hits.Length();
            for (int i = 0; i < size; i++)
            {
                doc = hits.Doc(i);
                indexIdStr += doc.Get("indexId") + "|"; //文档ID 列表
                searchContent += highlighter.GetBestFragment(y, "content", doc.Get("content")) + "|"; //搜索结果 列表
            }
            searcher.Close();
            if (size > 0)
            {
                ht.Add("indexIdStr", indexIdStr);
                ht.Add("searchContent", searchContent);
                ht.Add("result", "1");
            }
            else
            {
                ht.Add("result", "0"); //无结果
            }
            return ht;
        }

        //分词
        public static IList<string> segmentationWords2(string words)
        {
            IList<string> wordList = new List<string>();
            Participle p = new Participle();
            p.Init(@"C:\dicts");
            string outstr = string.Empty;
            outstr = p.TextSpliter(words);
            string[] wordArray = outstr.Split(new char[1] { ' ' });
            foreach (string word in wordArray)
            {
                if (word.Length > 1)
                {
                    if (!wordList.Contains(word))
                        wordList.Add(word);
                }
            }
            return wordList;
        }

    }
}
